package com.zyg.cod.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zyg.cod.test.KeyMapEnum;
import com.zyg.cod.test.Student;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Dsc:
 * @Author: zyg
 * @Date: 2021/1/26
 */
public class TestUtil {
	/**
	 * Json to xml string.
	 *
	 * @param json the json
	 * @return the string
	 */
	public static String jsonToXml(String json) {
		try {
			StringBuffer buffer = new StringBuffer();
			//buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
			buffer.append("自定义2222222222表头");
			JSONObject jObj = JSON.parseObject(json);
			jsonToXmlstr(jObj, buffer);
			return buffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}


	/**
	 * Json to xmlstr string.
	 *
	 * @param jObj   the j obj
	 * @param buffer the buffer
	 * @return the string
	 */
	public static String jsonToXmlstr(JSONObject jObj, StringBuffer buffer) {
		Set<Map.Entry<String, Object>> se = jObj.entrySet();
		for (Iterator<Map.Entry<String, Object>> it = se.iterator(); it.hasNext(); ) {
			Map.Entry<String, Object> en = it.next();
			if (en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONObject")) {
				buffer.append("<" + en.getKey() + ">");
				JSONObject jo = jObj.getJSONObject(en.getKey());
				jsonToXmlstr(jo, buffer);
				buffer.append("</" + en.getKey() + ">");
			} else if (en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONArray")) {
				JSONArray jarray = jObj.getJSONArray(en.getKey());
				for (int i = 0; i < jarray.size(); i++) {
					buffer.append("<" + en.getKey() + ">");
					JSONObject jsonobject = jarray.getJSONObject(i);
					jsonToXmlstr(jsonobject, buffer);
					buffer.append("</" + en.getKey() + ">");
				}
			} else if (en.getValue().getClass().getName().equals("java.lang.String")) {
				buffer.append("<" + en.getKey() + ">" + en.getValue());
				buffer.append("</" + en.getKey() + ">");
			}
		}
		return buffer.toString();
	}

	/**
	 * The entry point of application.
	 *
	 * @param args the input arguments
	 */
	public static void main(String[] args) {
		String xmlstr = jsonToXml("{\"class\":{\"student\":[{\"age\":\"89\",\"gender\":\"男\",\"name\":\"li三\",\"qk\":[{\"q1\":\"001\",\"q2\":\"002\",\"q3\":\"003\"},{\"q1\":\"001\",\"q2\":\"002\",\"q3\":\"003\"}]},{\"age\":\"17\",\"gender\":\"男\",\"name\":\"李四\",\"qk\":{\"q1\":\"005\",\"q2\":\"006\",\"q3\":\"007\"}},{\"age\":\"19\",\"gender\":\"女\",\"name\":\"王五\",\"qk\":{\"q1\":\"008\",\"q2\":\"009\",\"q3\":\"010\"}}]},\"class\":{\"student1\":[{\"age1\":\"89\",\"gender1\":\"男\",\"name1\":\"li三\",\"qk1\":[{\"q1\":\"001\",\"q2\":\"002\",\"q3\":\"003\"},{\"q1\":\"001\",\"q2\":\"002\",\"q3\":\"003\"}]},{\"age1\":\"17\",\"gender1\":\"男\",\"name1\":\"李四\",\"qk1\":{\"q1\":\"005\",\"q2\":\"006\",\"q3\":\"007\"}},{\"age1\":\"19\",\"gender1\":\"女\",\"name1\":\"王五\",\"qk1\":{\"q1\":\"008\",\"q2\":\"009\",\"q3\":\"010\"}}]},\"class\":{\"student\":[{\"age\":\"89\",\"gender\":\"男\",\"name\":\"li三\",\"qk\":[{\"q1\":\"001\",\"q2\":\"002\",\"q3\":\"003\"},{\"q1\":\"001\",\"q2\":\"002\",\"q3\":\"003\"}]},{\"age\":\"17\",\"gender\":\"男\",\"name\":\"李四\",\"qk\":{\"q1\":\"005\",\"q2\":\"006\",\"q3\":\"007\"}},{\"age\":\"19\",\"gender\":\"女\",\"name\":\"王五\",\"qk\":{\"q1\":\"008\",\"q2\":\"009\",\"q3\":\"010\"}}]},\"class\":{\"student1\":[{\"age1\":\"89\",\"gender1\":\"男\",\"name1\":\"li三\",\"qk1\":[{\"q1\":\"001\",\"q2\":\"002\",\"q3\":\"003\"},{\"q1\":\"001\",\"q2\":\"002\",\"q3\":\"003\"}]},{\"age1\":\"17\",\"gender1\":\"男\",\"name1\":\"李四\",\"qk1\":{\"q1\":\"005\",\"q2\":\"006\",\"q3\":\"007\"}},{\"age1\":\"19\",\"gender1\":\"女\",\"name1\":\"王五\",\"qk1\":{\"q1\":\"008\",\"q2\":\"009\",\"q3\":\"010\"}}]}}");
		System.out.println(xmlstr);
	}

	/**
	 * 自定义生成xml： txt载体
	 * 大类：段：信息体：一对多子类
	 */
	public static void toXml(Map<String, Object> map) {

		//获取当前大类名：Student
		String cateName = "Student";
		//文件没那个生成规则：
		String fileName = "K202101260001" + ".text";
		//文件生成路径
		String filePath = "E:/" + fileName;

		//获取数据
		List<Student> o = (List<Student>) map.get(KeyMapEnum.Student.getName());

		Element element = null;
		// 建立document对象，根节点
		element = DocumentHelper.createElement("Document");
		for (Student student : o) {
			Element element2 = element.addElement("EnBasInf");
			element2.addElement("EntName").setText("XXXXX公司");
			Element element3 = element2.addElement("MmbInf");
			element3.addElement("MmbAlias").setText("人员姓名");
			element3.addElement("age").setText("23");
			element3.addElement("test").setText("88888");
			element3.addElement("test2").setText("");
		}


		// 加入一行注释
		//element = DocumentHelper.createElement("Document");
		//2、二级节点

		//4、添加值


		//去掉文件头
		try {
			XMLWriter writer = new XMLWriter(new FileWriter(new File(filePath)));
			writer.write(element); //写入
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
